package com.ostrich.cloud.ostrichPermission.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ostrich.cloud.ostrichPermission.infra.mapper.*;
import com.ostrich.cloud.ostrichPermission.infra.model.*;
import com.ostrich.cloud.ostrichPermission.service.UserParentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2022-10-26
 */
@Service
@Slf4j
public class UserParentServiceImpl extends ServiceImpl<UserParentMapper, UserParent> implements UserParentService {
    @Resource
    private UserParentMapper userParentMapper;
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RolePermMapper rolePermMapper;
    @Resource
    private PermMapper permMapper;
    @Override
    @Transactional
    public Integer addUser(UserParent user, Integer roleId) {
        //List<UserParent> userParents = selectUserByUsernameAndPassword(user.getUsername());
        List<UserParent> userParents = selectUserByUsername(user.getUsername());
       if(userParents.size()==0){
           int insert = userParentMapper.insert(user);
           if(insert>0){
               List<UserParent> suserParents = selectUserByUsername(user.getUsername());
               UserParent suser = suserParents.get(0);
               Integer parentId = suser.getParentId();
               UserRole ur=new UserRole();
               ur.setRoleId(roleId);
               ur.setParentId(parentId);
               int insert1 = userRoleMapper.insert(ur);
               if(insert1>0){
                   return parentId;
               }else {
                   log.info("用户角色添加失败");
                   return 0;

               }
           }else{
               log.info("用户添加失败");
               return 0;

           }
       }else if(userParents.size()>0){
           log.info("用户已经存在");
           return 0;
       }
        return 0;
    }

    @Override
    public List<UserParent> selectUserByUsername(String username) {
        QueryWrapper<UserParent> qw=new QueryWrapper<>();
        qw.eq("username",username);
        List<UserParent> userParents = userParentMapper.selectList(qw);
        return userParents;
    }

    @Override
    public UserParent selectUserByPid(Integer pid) {
        UserParent userParent = userParentMapper.selectById(pid);
        return userParent;
    }

    @Override
    public String updateUserStatus(Integer pid, String status) {
        UserParent userParent = selectUserByPid(pid);
        if(userParent==null){
            log.info("该用户不存在");
            return "该用户不存在";
        }else{
            userParent.setStatus(status);
            int i = userParentMapper.updateById(userParent);
            if(i>0){
                log.info("修改成功");
                return "修改成功";
            }
            return "修改用户状态失败";
        }
    }

    @Override
    public List<Role> selctRoleListByPid(Integer pid) {
        QueryWrapper qw=new QueryWrapper();
        qw.eq("parent_id",pid);
        List<UserRole> userRoles = userRoleMapper.selectList(qw);
        log.info("{}",userRoles);
        List<Role> roles=new ArrayList<>();
        for(int i=0;i<userRoles.size();i++ ){
            UserRole userRole = userRoles.get(i);
            QueryWrapper<Role> qw1=new QueryWrapper<>();
            qw1.eq("role_id",userRole.getRoleId());
            List<Role> roles1 = roleMapper.selectList(qw1);
            roles.addAll(roles1);
        }
        log.info("查询角色列表：{}",roles);
        return roles;
    }

    @Override
    public List<Perm> getPermissionListByPid(Integer pid) {
        //根据pid查询角色列表
        List<Role> roles = selctRoleListByPid(pid);
        List<RolePerm> rplist=new ArrayList<>();
        //查询角色权限联系列表
        for(int i=0;i<roles.size();i++){
            Role role = roles.get(i);
            QueryWrapper<RolePerm> qw=new QueryWrapper<>();
            qw.eq("role_id",role.getRoleId());
            List<RolePerm> rolePerms = rolePermMapper.selectList(qw);
            rplist.addAll(rolePerms);
        }
        //根据角色权限列表查询权限列表
        List<Perm> permlist=new ArrayList<>();
        for(int i=0;i<rplist.size();i++){
            RolePerm rolePerm = rplist.get(i);
            QueryWrapper<Perm> qw1=new QueryWrapper<>();
            qw1.eq("perm_id",rolePerm.getPermId());
            List<Perm> permList = permMapper.selectList(qw1);
            permlist.addAll(permList);
        }

        return permlist;
    }
}
